<?php
namespace app\common\controller;

/**
 * 抄送类
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/10/08
 * Time: 11:41
 */
class Copy{

    /**
     * 生成抄送（未抄送）
     * @param $from_openid
     * @param $sourceid
     * @param $source
     * @param $copy_openid
     * @return int
     */
    public function gen_copy_not($from_openid,$sourceid,$source,$copy_openid){
        $name = $this->deal_copy_name($sourceid,$source);

        $maps = [];
        $maps[] = ['from_openid','=',$from_openid];
        $maps[] = ['sourceid','=',$sourceid];
        $maps[] = ['source','=',$source];
        $maps[] = ['status','=',0];
        $old_copy_openid = model('copy')->get_column($maps,'copy_openid');

        //求新抄送用户和已有抄送用户的交集
        $intersect_copy_openid = array_intersect($copy_openid,$old_copy_openid);

        //删除没有的抄送用户
        $del_copy_openid = array_diff($old_copy_openid,$intersect_copy_openid);
        $maps = [];
        $maps[] = ['from_openid','=',$from_openid];
        $maps[] = ['sourceid','=',$sourceid];
        $maps[] = ['source','=',$source];
        $maps[] = ['status','=',0];
        $maps[] = ['copy_openid','in',$del_copy_openid];
        $cids = model('copy')->get_column($maps,'id');
        if(!empty($cids)){
            $r = model('copy')->destroy($cids);
        }

        //添加新的抄送用户
        $add_copy_openid = array_diff($copy_openid,$intersect_copy_openid);
        foreach($add_copy_openid as $k => $v){
            $c_data = [];
            $c_data['from_openid'] = $from_openid;
            $c_data['sourceid'] = $sourceid;
            $c_data['source'] = $source;
            $c_data['name'] = $name;
            $c_data['copy_openid'] = $v;
            $r = model('copy')->insert($c_data);
        }
        return 1;
    }

    /**
     * 修改抄送（已抄送）
     * @param $from_openid
     * @param $sourceid
     * @param $source
     * @return int
     */
    public function save_copy_open($from_openid,$sourceid,$source){
        $maps = [];
        $maps[] = ['from_openid','=',$from_openid];
        $maps[] = ['sourceid','=',$sourceid];
        $maps[] = ['source','=',$source];
        $cids = model('copy')->get_column($maps,'id');
        foreach($cids as $k => $v){
            $c_data = [];
            $c_data['status'] = 1;

            $maps = [];
            $maps[] = ['id','=',$v];
            $r = model('copy')->where($maps)->update($c_data);

            //发送消息
            $r = $this->send_copy_msg($v,'请您关注！！！');
        }
        return 1;
    }

    /**
     * 处理抄送名称
     * @param $sourceid
     * @param $source 1：巡店；2：内部协同；3：推广工作；4：工作日志；5：活动开展；6：通知公告
     * @return int|string
     */
    public function deal_copy_name($sourceid,$source){
        $name = '';
        switch($source){
            case 1:
                $maps = [];
                $maps[] = ['id','=',$sourceid];
                $crm_detail = model('cp_region_modular')->get_detail($maps,'id,branchid,floorid,regionid,modularid,action');

                //查找分部
                $maps = [];
                $maps[] = ['id','=',$crm_detail['branchid']];
                $b_detail = model('branch')->get_detail($maps,'id,name');

                //查找楼层
                $maps = [];
                $maps[] = ['id','=',$crm_detail['floorid']];
                $f_detail = model('floor')->get_detail($maps,'id,name');

                //查找区域
                $maps = [];
                $maps[] = ['id','=',$crm_detail['regionid']];
                $r_detail = model('region')->get_detail($maps,'id,name');

                //查找模块
                $maps = [];
                $maps[] = ['id','=',$crm_detail['modularid']];
                $m_detail = model('modular')->get_detail($maps,'id,name');

                $name = '巡店-' . $b_detail['name'] . '-' . $f_detail['name'] . '-' . $r_detail['name'] . '-' . $m_detail['name'];
                break;
            case 2:
                $maps = [];
                $maps[] = ['id','=',$sourceid];
                $wrx_detail = model('work_record_xt')->get_detail($maps,'id,name');
                $name = '内部协同-' . $wrx_detail['name'];
                break;
            case 3:
                $maps = [];
                $maps[] = ['id','=',$sourceid];
                $wre_detail = model('work_record_extend')->get_detail($maps,'id,name');
                $name = '推广工作-' . $wre_detail['name'];
                break;
            case 4:
                $maps = [];
                $maps[] = ['id','=',$sourceid];
                $wrd_detail = model('work_record_daily')->get_detail($maps,'id,name');
                $name = '工作日志-' . $wrd_detail['name'];
                break;
            case 5:
                $maps = [];
                $maps[] = ['id','=',$sourceid];
                $wra_detail = model('work_record_active')->get_detail($maps,'id,name');
                $name = '活动开展-' . $wra_detail['name'];
                break;
            case 6:
                $maps = [];
                $maps[] = ['id','=',$sourceid];
                $wrn_detail = model('work_record_notice')->get_detail($maps,'id,name');
                $name = '通知公告-' . $wrn_detail['name'];
                break;
            default:
                return 0;
        }
        return $name;
    }

    /**
     * 发送抄送消息
     * @param $copyid
     * @param $remark
     * @return int
     */
    public function send_copy_msg($copyid,$remark){
        $maps = [];
        $maps[] = ['id','=',$copyid];
        $c_detail = model('copy')->get_detail($maps,'id,from_openid,name,copy_openid');

        //查找用户
        $maps = [];
        $maps[] = ['openid','=',$c_detail['from_openid']];
        $u_detail = model('user')->get_detail($maps,'id,username,tel');

        //发送消息
        $sm = new SendMsg();
        $r = $sm->send_copy_msg($c_detail['copy_openid'],$c_detail['name'],$u_detail['username'],$u_detail['tel'],$remark);
        return 1;
    }

    /**
     * 发送批复消息
     * @param $copyid
     * @return int
     */
    public function send_reply_msg($copyid){
        $maps = [];
        $maps[] = ['id','=',$copyid];
        $c_detail = model('copy')->get_detail($maps,'id,from_openid,name,copy_openid,explain');

        //查找用户
        $maps = [];
        $maps[] = ['openid','=',$c_detail['copy_openid']];
        $u_detail = model('user')->get_detail($maps,'id,username,tel');

        //发送消息
        $sm = new SendMsg();
        $r = $sm->send_reply_msg($c_detail['from_openid'],$c_detail['name'],$u_detail['username'],$u_detail['tel'],$c_detail['explain']);
        return 1;
    }
}